
#include "OptionsState.h"
#include "../../SGD Wrappers/CSGD_Direct3D.h"
#include "../../SGD Wrappers/CSGD_DirectInput.h"
#include "../../SGD Wrappers/CSGD_XAudio2.h"
#include "MainMenuState.h"
#include "Game.h"
#include "BitmapFont.h"
#include <cassert>
#include "tinyxml.h"


//Singleton accessor
OptionsState* OptionsState::GetInstance(void)
{
	static OptionsState instance;
	return &instance;
}


//Constructor
OptionsState::OptionsState(void)
{
	m_nCursor = 0;
}


//Destructor
OptionsState::~OptionsState(void)
{
}


//Enter
void OptionsState::Enter(void)
{
	// Keep the same cursor position from last time
	menuSoundID = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("../resource/sounds/PlaceholderSound.wav"));
}


//Exit
void OptionsState::Exit (void)
{
	SaveSoundOptions();
	CSGD_XAudio2::GetInstance()->SFXStopSound(menuSoundID);
	CSGD_XAudio2::GetInstance()->SFXUnloadSound(menuSoundID);
	menuSoundID = -1;
}


//Input
bool OptionsState::Input(void)
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();
	CSGD_XAudio2* XA = CSGD_XAudio2::GetInstance();
	static int lastMouseY = 0;

	//Ignore input when alt is down
	if (pDI->KeyDown(DIK_LALT) == true || pDI->KeyDown(DIK_RALT) == true)
		return true;

	if (pDI->KeyPressed(DIK_ESCAPE) || pDI->MouseButtonPressed(1))
	{
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
		return true;
	}


	if (pDI->KeyPressed(DIK_UP) == true)
	{
		m_nCursor--;
		if( m_nCursor < 0 )
			m_nCursor = MENU_ITEMCOUNT-1;
		CSGD_XAudio2::GetInstance()->SFXPlaySound(menuSoundID);
		lastMouseY = pDI->MouseGetPosY();
	}


	if (pDI->KeyPressed(DIK_DOWN) == true)
	{
		m_nCursor++;
		if (m_nCursor >= MENU_ITEMCOUNT)
			m_nCursor = 0;
		CSGD_XAudio2::GetInstance()->SFXPlaySound(menuSoundID);
		lastMouseY = pDI->MouseGetPosY();
	}


	if (pDI->KeyPressed(DIK_LEFT) == true)
	{
		switch (m_nCursor)
		{
		case MENU_SOUND:
			{
				float vol = XA->SFXGetMasterVolume();
				vol -= 0.1f;
				if (vol <= 0.0f)
					vol = 0.0f;
				XA->SFXSetMasterVolume(vol);
				CSGD_XAudio2::GetInstance()->SFXPlaySound(menuSoundID);
			}
			break;

		case MENU_MUSIC:
			{
				float vol = XA->MusicGetMasterVolume();
				vol -= 0.1f;
				if (vol <= 0.0f)
					vol = 0.0f;
				XA->MusicSetMasterVolume(vol);
			}
			break;
		}
	}


	if (pDI->KeyPressed(DIK_RIGHT) == true)
	{
		switch (m_nCursor)
		{
		case MENU_SOUND:
			{
				float vol = XA->SFXGetMasterVolume();
				vol += 0.1f;
				if (vol >= 1.0f)
					vol = 1.0f;
				XA->SFXSetMasterVolume(vol);
				CSGD_XAudio2::GetInstance()->SFXPlaySound(menuSoundID);
			}
			break;

		case MENU_MUSIC:
			{
				float vol = XA->MusicGetMasterVolume();
				vol += 0.1f;
				if (vol >= 1.0f)
					vol = 1.0f;
				XA->MusicSetMasterVolume(vol);
			}
			break;
		}
	}


	if (pDI->KeyPressed(DIK_RETURN) || pDI->MouseButtonPressed(0))
	{
		// What menu option is selected?
		switch (m_nCursor)
		{
		case MENU_BACK:
			{
				Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
				return true;
			}
			break;
		}
	}

	int lastCursor = m_nCursor;
	int y = pDI->MouseGetPosY();
	if (y != lastMouseY)
	{
		lastMouseY = 0;
		int initialY = 280;
		int currentY = initialY;
		int offsetY = 35;
		if (y > currentY && y < currentY+offsetY) 
			m_nCursor = MENU_SOUND;
		currentY += offsetY;
		if (y > currentY && y < currentY+offsetY) 
			m_nCursor = MENU_MUSIC;
		currentY += offsetY;
		if (y > currentY && y < currentY+offsetY) 
			m_nCursor = MENU_BACK;
		currentY += offsetY;
	}

	if (m_nCursor != lastCursor)
		CSGD_XAudio2::GetInstance()->SFXPlaySound(menuSoundID);

	return true; //Keep running the game
}


//Update
void OptionsState::Update(float dt)
{
}


//Render
void OptionsState::Render(void)
{
	CSGD_XAudio2* XA = CSGD_XAudio2::GetInstance();

	CBitmapFont* pFont = Game::GetInstance()->GetFont();
	pFont->Draw(_T("Options"), 260, 170, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 255, 255 ) );

	int initialX = 310;
	int initialY = 280;
	int currentY = initialY;
	int offsetY  = 35;
	DWORD color  = D3DCOLOR_ARGB( 255, 255, 255, 255 );
	DWORD color2 = D3DCOLOR_ARGB( 110, 255, 255, 255 );

	//Sound option
	pFont->Draw(_T("Sound"), initialX, currentY, 0.8f, color);
	if (m_nCursor == MENU_SOUND)
	{
		TOSTRINGSTREAM output;
		output << _T("< ") << (int)((XA->SFXGetMasterVolume()*100.0f)+0.1f) << _T("% >");
		pFont->Draw(output.str().c_str(), initialX+150, currentY, 0.8f, color);
	}
	else
	{
		TOSTRINGSTREAM output;
		output << _T("  ") << (int)((XA->SFXGetMasterVolume()*100.0f)+0.1f) << _T("%  ");
		pFont->Draw(output.str().c_str(), initialX+150, currentY, 0.8f, color2);
	}

	//Music option
	currentY += offsetY;
	pFont->Draw(_T("Music"), initialX, currentY, 0.8f, color);
	if (m_nCursor == MENU_MUSIC)
	{
		TOSTRINGSTREAM output;
		output << _T("< ") << (int)((XA->MusicGetMasterVolume()*100.0f)+0.1f) << _T("% >");
		pFont->Draw(output.str().c_str(), initialX+150, currentY, 0.8f, color);
	}
	else
	{
		TOSTRINGSTREAM output;
		output << _T("  ") << (int)((XA->MusicGetMasterVolume()*100.0f)+0.1f) << _T("%  ");
		pFont->Draw(output.str().c_str(), initialX+150, currentY, 0.8f, color2);
	}

	//Back to main menu
	currentY += offsetY;
	pFont->Draw(_T("Back"), initialX, currentY, 0.8f, color);

	//Cursor
	pFont->Draw(_T(">"), initialX-50, initialY + (offsetY * m_nCursor), 0.8f, color);
}


//Save sound options
void OptionsState::SaveSoundOptions(void) const
{
	CSGD_XAudio2* XA = CSGD_XAudio2::GetInstance();

	TiXmlDocument document;
	TiXmlDeclaration* declaration = new TiXmlDeclaration("1.0", "UTF-8", "");
	document.LinkEndChild(declaration);
	TiXmlElement* root = new TiXmlElement("SoundOptions");
	document.LinkEndChild(root);

	TiXmlElement* sfx = new TiXmlElement("Sound");
	sfx->SetAttribute("VolumePercent",   (int)(XA->SFXGetMasterVolume()*100.01f));
	root->LinkEndChild(sfx);

	TiXmlElement* music = new TiXmlElement("Music");
	music->SetAttribute("VolumePercent", (int)(XA->MusicGetMasterVolume()*100.01f));
	root->LinkEndChild(music);

	document.SaveFile("SoundOptions.xml");
}
